import loginHtml from 'ngtemplate!html!../../../tpls/dialog/login.dialog.html';

class AppController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $interval, $state, ngDialog, cfpLoadingBar, dialogService, arrayService, UserService, authService, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
    	vm.$interval 	 = $interval;
        vm.$state        = $state;
    	vm.ngDialog   	 = ngDialog;
    	vm.cfpLoadingBar = cfpLoadingBar;
    	vm.dialogService = dialogService;
    	vm.arrayService  = arrayService;
        vm.UserService   = UserService;
    	vm.authService   = authService;
    	vm.iRunner 	     = iRunner;

        console.log('AppController');

        // $rootScope.logo2  = location.pathname + 'images/logo2.png';
        // $rootScope.logo3  = location.pathname + 'images/logo3.png';
        // $rootScope.logo4  = location.pathname + 'images/logo4.png';
        // $rootScope.image5 = location.pathname + 'images/5.png';

        //读取是否是管理员true=>管理员，未读到则是false=>非管理员
        //默认非管理员
        $rootScope.is_admin = false;
        if (vm.$cookieStore.get('is_admin')) {
            $rootScope.is_admin = true;
        }

        // loading
        $rootScope.start = () => {
            cfpLoadingBar.start();
        };

        $rootScope.complete = () => {
            cfpLoadingBar.complete();
        };

        // fake the initial load so first time users can see the bar right away:
        $rootScope.start();
        $rootScope.complete();
        
        // 缓存浏览器可视高度
        $rootScope.winHeight = window.innerHeight;

        // 阻止事件冒泡
        $rootScope.stopPropagation = ($event) => {
            $event.stopPropagation();
        }

        // 上移
        $rootScope.upRecord = (arr, $index) => {
            vm.arrayService.upRecord(arr, $index);
        }
     
        // 下移
        $rootScope.downRecord = (arr, $index) => {
            vm.arrayService.downRecord(arr, $index);
        }

        // 删除
        $rootScope.deleteItem = (arr, $index) => {
            vm.arrayService.deleteItem(arr, $index);
        }

        // 用户登出
        $rootScope.logout = () => {
            $interval.cancel(interval);
            vm.UserService.postLogout(vm.$cookieStore.get('access_token'))
            .then((data) => {

                // 成功调用
                if(data.code==0) {
                    // 取消管理员权限
                    $rootScope.is_admin = false;
                    vm.$cookieStore.put("is_admin", false);

                    vm.$cookieStore.remove('access_token');
                    vm.dialogService.success('登出成功', 'loginindex.login');
                }

                console.log(data)
            }, (data) => {
                console.log('error')
            });
        }

        // 初始化表单数据
        $rootScope.user = {
            username: '',
            password: ''
        }

        // 登录模态框
        function loginDialog() {
            vm.ngDialog.open({
                template: loginHtml,
                className: 'ngdialog-theme-default ngdialog-alert ngdialog-login',
                scope: $scope
            });
        }

        // 登录超时验证
        function checkLogin() {

            if (!vm.$cookieStore.get('access_token')) {
                return false
            }

            vm.UserService.postCheckLogin(vm.$cookieStore.get('access_token'))
            .then( data => {
                if (!data.data.is_login) {
                    if (location.hash.indexOf('login')==-1&&location.hash.indexOf('findpass')==-1&&location.hash.indexOf('register')==-1&&location.hash.indexOf('modifypass')==-1) {
                        loginDialog();
                        $interval.cancel(interval)
                    }
                }
            }, error => console.log('error'))
        }

        // 10s间隔请求判断登录超时
        var interval = $interval( () => checkLogin(), 10000)


        // 提交表单
        $rootScope.submitForm = (isValid) => {

            var vm = this;

            // 验证失败
            if (!isValid) {
                
                $rootScope.error = '用户名或密码错误';
                vm.$timeout(() => $rootScope.error = '', 1500)

            }

            // 验证成功
            if (isValid) {

                // 拼接POST数据格式
                var params = {
                    cellphone: $rootScope.user.username,
                    password: $rootScope.user.password
                }

                console.log(params)

                // 用户登录
                vm.UserService.postLogin(params)
                .then( data => {

                    // 判断是否成功调用
                    if (data.code==0) {
                        vm.$cookieStore.put('access_token', data.data.access_token);
                        vm.ngDialog.close();
                        interval = $interval( () => checkLogin(), 10000)
                    }
                    
                    // 判断是否登录失败
                    if(data.code!=0) {
                        $rootScope.error = '用户名或密码错误';
                        vm.$timeout(() => $rootScope.error = '', 1500)
                    }

                    console.log(data)
                    
                }, error => console.log('error'))
            }

            return false;
        }
    }

}

AppController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$interval', '$state', 'ngDialog', 'cfpLoadingBar', 'dialogService', 'arrayService', 'UserService', 'authService', 'iRunner']; 

export default AppController;